﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Physio10Context;
using DatabaseManager;

namespace Physio_Clinic
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Drugorder m_order = null;
        Booking m_booking = null;
        public Qf8soUser currentUser = null;
        int m_bookId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["currentUser"] != null)
            {
                currentUser = (Qf8soUser)Session["currentUser"];
            }

            DateRelease.Text = DateTime.Today.ToShortDateString();

            if (Request.QueryString["cashPayment"] != null)
            {
                Session["bookId"] = null;
            }

            if (Request.QueryString["bookId"] != null || Session["bookId"] != null)
            {
                if (Request.QueryString["bookId"] != null)
                    m_bookId = Convert.ToInt32(Request.QueryString["bookId"]);
                else
                    m_bookId = Convert.ToInt32(Session["bookId"]);

                m_booking = PersistentManager.Instance.GetBooking(m_bookId);

                if (m_booking != null)
                {
                    m_order = PersistentManager.Instance.GetDrugOrderbyBook(m_bookId);

                    if (m_order == null)
                    {
                        m_order = new Drugorder();
                        m_order.BookId = m_bookId;
                        m_order.Discount = "";


                        PersistentManager.Instance.Add(m_order, currentUser.Username);
                        PersistentManager.Instance.Save();
                    }

                    m_order = PersistentManager.Instance.GetDrugOrderbyBook(m_bookId);

                    if (Request.QueryString["bookId"] != null)
                        Session["bookId"] = Request.QueryString["bookId"];
                }
                else
                {

                }


            }

            bool isCashPayment = false;

            if (currentUser != null && currentUser.Username == "admin")
            {

                if (m_booking == null)
                {
                    m_booking = new Booking();
                    m_booking.UserId = currentUser.Id;
                    m_booking.UserName = "จ่ายสด";
                    m_booking.AppointmentTime = DateTime.Today;
                    m_booking.EndappointmentTime = DateTime.Today;

                    PersistentManager.Instance.Add(m_booking);
                    PersistentManager.Instance.Save();
                    isCashPayment = true;
                }

                if (m_order == null)
                {
                    m_order = new Drugorder();
                    m_order.BookId = m_booking.BookId;
                    m_order.Discount = "";
                    m_order.DateDefineProduct = "";

                    PersistentManager.Instance.Add(m_order, currentUser.Username);
                    PersistentManager.Instance.Save();

                    Session["bookId"] = m_booking.BookId;
                }

                if (!Page.IsPostBack)
                {
                    BarcodeId.Text = "";
                    DiscountTextbox.Text = m_order.Discount;

                    TaxDateText.Text = m_order.DateDefineProduct;
                }

                if (m_booking.Qf8soUser != null)
                    CustomerName.Text = m_booking.Qf8soUser.Name;
                else
                    CustomerName.Text = "จ่ายสด";

                BarcodeId.Focus();
                LinqDataSource.DataBind();

                Session["orderId"] = m_order.OrderId;
                decimal total_Price = m_order.ReEvalTotalPrice();

                totalPrice.Text = total_Price.ToString("N2");
                //DiscountTextbox.Text = m_order.Discount;
                taxPrice.Text = m_order.Vat.ToString("N2");
                AllTotalPrice.Text = m_order.AllTotalPrice.ToString("N2");
                OrderNumberText.Text = m_order.GetOrderNumber();

                TaxNumberText.Text = m_order.GetTaxNumber();
                //TaxDateText.Text = m_order.DateDefineProduct;

                totalPriceAfterDiscount.Text = m_order.PriceAfterDiscount.ToString("N2");

                if (m_order.GetOrderCount() == 0 && isCashPayment == false)
                {
                    foreach (Drug drug in PersistentManager.Instance.GetAllMedicalService())
                    {
                        if (drug != null)
                        {
                            Drugorderdetail order_detail = new Drugorderdetail();
                            order_detail.DrugOrderId = m_order.OrderId;
                            order_detail.DrugId = drug.DrugId;
                            order_detail.Amount = 1;
                            order_detail.Discount = "";
                            drug.DecreaseAmount();
                            PersistentManager.Instance.Add(order_detail, currentUser.Username);
                            PersistentManager.Instance.Save();
                        }
                    }

                    if (m_order != null)
                    {
                        Session["orderId"] = m_order.OrderId;
                        Response.Redirect("Invoice.aspx?bookId=" + m_order.BookId.ToString());
                    }
                }

            }

            Session["DrugOrder"] = m_order;
            Session["Booking"] = m_booking;
            if (m_booking != null)
            Session["bookId"] = m_booking.BookId;
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            m_order.DateDefineProduct = TaxDateText.Text.ToString();
            //PersistentManager.Instance.Save();
            Response.Redirect("Report.aspx");
        }

        protected void TextBox7_TextChanged(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           m_bookId =  (int)Session["bookId"];
            m_order = PersistentManager.Instance.GetDrugOrderbyBook(m_bookId);
            m_order.DateDefineProduct = TaxDateText.ToString();

            if (BarcodeId.Text == "")
                return;

            Drug drug = PersistentManager.Instance.GetDrug(BarcodeId.Text);

            if (drug != null)
            {
                Drugorderdetail order_detail = new Drugorderdetail();
                order_detail.DrugOrderId = m_order.OrderId;
                order_detail.DrugId = drug.DrugId;
                order_detail.Amount = 1;
                order_detail.Discount = "";
                drug.DecreaseAmount();
                PersistentManager.Instance.Add(order_detail, currentUser.Username);
                PersistentManager.Instance.Save();
            }

            if (m_order != null)
            {
                Session["orderId"] = m_order.OrderId;
                Response.Redirect("Invoice.aspx?bookId=" + m_order.BookId.ToString());   
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedRow != null)
            {
                string value1 = GridView1.SelectedRow.Cells[2].Text;

                Drugorderdetail select_row = PersistentManager.Instance.GetDrugOrderDetail(Convert.ToInt32(value1));
                //current_drug = PersistentManager.Instance.GetDrug(Convert.ToInt32(value1));

                Session["currentOrderDetail"] = select_row;

                Button3.Enabled = true;
                Button4.Enabled = true;

            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Session["currentOrderDetail"] != null)
            {
                Response.Redirect("./InvoiceDetail.aspx");
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (Session["currentOrderDetail"] != null)
            {
                PersistentManager.Instance.Delete((Drugorderdetail)Session["currentOrderDetail"], currentUser.Username);
                PersistentManager.Instance.Save();
            }

            Response.Redirect("./Invoice.aspx");
        }

        protected void DiscountTextbox_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            m_order.Discount = DiscountTextbox.Text;

            PersistentManager.Instance.Save();

            Response.Redirect("./Invoice.aspx");
        }

        protected void Button6_Click(object sender, EventArgs e)
        {

        }

        protected void Button6_Click1(object sender, EventArgs e)
        {
            m_order = PersistentManager.Instance.GetDrugOrderbyBook((int)Session["bookId"]);

            Drug drug = PersistentManager.Instance.GetDrug(Convert.ToInt32(DropDownList2.SelectedValue));

            if (drug != null)
            {
                Drugorderdetail order_detail = new Drugorderdetail();
                order_detail.DrugOrderId = m_order.OrderId;
                order_detail.DrugId = drug.DrugId;
                order_detail.Amount = 1;
                order_detail.Discount = "";
                drug.DecreaseAmount();
                PersistentManager.Instance.Add(order_detail, currentUser.Username);
                PersistentManager.Instance.Save();
            }

            if (m_order != null)
            {
                Session["orderId"] = m_order.OrderId;
                Response.Redirect("Invoice.aspx?bookId=" + m_order.BookId.ToString());
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string value = (string)e.CommandArgument;

            Drugorderdetail select_row = PersistentManager.Instance.GetDrugOrderDetail(Convert.ToInt32(value));
            //current_drug = PersistentManager.Instance.GetDrug(Convert.ToInt32(value1));

            Session["currentOrderDetail"] = select_row;

            if (e.CommandName == "DeleteOrder")
            {
                if (Session["currentOrderDetail"] != null)
                {
                    PersistentManager.Instance.Delete((Drugorderdetail)Session["currentOrderDetail"], currentUser.Username);
                    PersistentManager.Instance.Save();
                }

                Response.Redirect("./Invoice.aspx");

            }
            else
            {
                if (Session["currentOrderDetail"] != null)
                {
                    Response.Redirect("./InvoiceDetail.aspx");
                }
            }
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button myButton = (Button)e.Row.Cells[1].Controls[0];
                Button myButton2 = (Button)e.Row.Cells[2].Controls[0];

                if (e.Row.DataItem != null)
                {
                    myButton.CommandArgument = ((Drugorderdetail)e.Row.DataItem).DrugOrderDetail.ToString();
                    myButton2.CommandArgument = ((Drugorderdetail)e.Row.DataItem).DrugOrderDetail.ToString();
                }
            }
        }
    }
}